package lvbu.wang.lvbuoperator.utils;

import android.util.Log;

import com.qing.library.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by DaiQing.Zha on 2016/11/1 0001.
 * 格式转化工具类
 */
public class FormatUtil {
    /**
     * 获取电机锁定状态
     * @param lockStatus
     * @return
     */
    public static boolean getLockStatus(String lockStatus){
        if ("01".equals(lockStatus)){
            return true;
        }
        if ("02".equals(lockStatus)){
            return false;
        }
        return false;
    }
    /**
     * str 转 float
     * @param str 原始字符串
     * @param decimalNum 小数位数
     * @return
     */
    public static float str2Float(String str,int decimalNum){
        float divisor = 10.0f;
        if (decimalNum == 1) divisor = 10.0f;
        if (decimalNum == 2) divisor = 100.00f;
        if (decimalNum == 3) divisor = 1000.000f;
        float result;
        try{
            result = Integer.parseInt(str,16) / divisor;
        }catch (Exception e){
            result = 0;
        }
        return result;
    }
    /**
     * str 转 float --- 带符号
     * @param str
     * @param decimalNum
     * @return
     */
    public static float str2FloatWithSign(String str,int decimalNum){
        char sign = str.charAt(0);
        float divisor = 10.0f;
        if (decimalNum == 1) divisor = 10.0f;
        if (decimalNum == 2) divisor = 100.00f;
        if (decimalNum == 3) divisor = 1000.000f;
        float result;
        try{
            if (sign == '0') {
                result = Integer.parseInt(str.substring(1,str.length()),16) / divisor;
            }else if (sign == '1') {
                result = -Integer.parseInt(str.substring(1,str.length()),16) / divisor;
            }else{
                result = 0;
            }
        }catch (Exception e){
            result = 0;
        }
        return result;
    }

    /**
     * 整形转换成16进制字符串
     * @param value
     * @return
     */
    public static String intToHex(int value){
        String str = Integer.toHexString(value);
        if (str.length() == 1){
            str = "0" + str;
        }
        return str;
    }

    static SimpleDateFormat df = new SimpleDateFormat("yyMMdd");//设置日期格式 example：2017-03-21 转换后：170321


    public static String getTimeD5(){
        String time;
        try{
            time = df.format(new Date());
            String year = StringHelper.subString(time,0,2);
            String month = StringHelper.subString(time,2,4);
            String day = StringHelper.subString(time,4,6);
            System.out.print("year = " + Integer.toHexString(Integer.valueOf(year)));
            time = getFrontCompleteStr(Integer.toHexString(Integer.valueOf(year)),2)
                    + getFrontCompleteStr(Integer.toHexString(Integer.valueOf(month)),2)
                    + getFrontCompleteStr(Integer.toHexString(Integer.valueOf(day)),2);
        }catch (Exception e){
            time = "";
        }
        return time;
    }

    /**
     * 转换时间
     * example:11050F转换成110515
     * @param time
     * @return
     */
    public static String parseTimeHexToInt(String time){
        String result = "";
        try{
            result = "" + getFrontCompleteStr(String.valueOf(Integer.parseInt(StringHelper.subString(time,0,2),16)),2)
                    + getFrontCompleteStr(String.valueOf(Integer.parseInt(StringHelper.subString(time,2,4),16)),2)
                    + getFrontCompleteStr(String.valueOf(Integer.parseInt(StringHelper.subString(time,4,6),16)),2);
        }catch (Exception e){

        }
        return result;
    }
    /**
     * 计算有效命令的长度（字节数）
     * @param validValue 传进来的有效命令
     * @return 返回命令长度的16进制形式
     * example：传入参数：validValue = "434731" 返回结果 03
     */
    public static String calculateValidCommandLength(String validValue){
        int valueLength = validValue.length() / 2;
        return getFrontCompleteStr(Integer.toHexString(valueLength),2); //16进制，两位
    }

    /**
     * 计算总的命令长度
     * @param command 传进来的命令
     * @return 该命令的长度（位数）
     * example：command = "1234567890" 返回结果16
     */
    public static int calculateSumCommandLength(String command){
        int commandLength = command.length() / (8 * 2);
        if (command.length() % (8 * 2) == 0){
            return commandLength * (8 * 2);
        }else{
            return (commandLength + 1) * (8 * 2);
        }
    }
    /**
     * 补全字符串
     * @param value 原字符串
     * @param number 补全后的字符串的个数
     * @return 补全后的字符串
     * example：value = "5" number = 2 return = "05
     */
    public static String getFrontCompleteStr(String value,int number){
        for (int i = 0; value.length() < number; i ++){
            value = "0" + value;
        }
        return value;
    }

    /**
     * 补全字符串
     * @param value 原字符串
     * @param number 补全后字符串的个数
     * @return 补全后的字符串
     * example: value = "5" number = 3 return = "500"
     */
    public static String getBehindCompleteStr(String value,int number){
        for (int i = 0; value.length() < number; i ++){
            value = value + "0";
        }
        return value;
    }
    /**
     * 转换蓝牙格式
     * @param value
     * @return
     */
    public static String getBluetooth(String value){
        String bluetooth = "";
        for (int i = 0; i < value.length(); i ++){
            bluetooth = bluetooth + StringHelper.subString(value,i,i + 2) + ":";
            i++;
        }
        return StringHelper.subString(bluetooth,0,17);
    }
}
